package com.lsege.controller.backstage;

import com.github.pagehelper.PageHelper;
import com.lsege.entity.JsonResult;
import com.lsege.entity.Page;
import com.lsege.entity.backstage.Delivery;
import com.lsege.service.backstage.DeliveryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by Administrator on 2017/7/6.
 */
@RestController
@RequestMapping("/delivery")
public class DeliveryController {
    @Autowired
    DeliveryService deliveryService;

    @GetMapping("queryDeliveryList")
    public JsonResult queryDeliveryList (Integer pageNum,Integer pageSize){
        JsonResult json = new JsonResult();
        try{
            PageHelper.startPage(pageNum,pageSize);
            List<Delivery> list = deliveryService.getDeliveryList();
            Long total = deliveryService.queryDeliveryCount();
            json.setData(new Page(list,total,pageSize,pageNum));
            json.setSuccess(true);
            json.setMessage("获取列表成功");
        }catch (Exception e){
            e.printStackTrace();
            json.setSuccess(false);
            json.setMessage("获取列表失败");
        }
        return json;
    }

    @PostMapping("addDelivery")
    public JsonResult addDelivery(Delivery delivery){
        JsonResult json=new JsonResult();
        Long num=deliveryService.getDeliveryCount(delivery);
        if (num == 0){
            try {
                deliveryService.addDelivery(delivery);
                json.setSuccess(true);
                json.setMessage("添加配送员成功");
            }catch (Exception e){
                e.printStackTrace();
                json.setSuccess(false);
                json.setMessage("添加配送员失败");
            }
        }else {
            json.setSuccess(false);
            json.setMessage("该配送员已存在");
        }
        return json;
    }

    @PostMapping("deleteDeliver")
    public JsonResult deleteDeliver(Delivery delivery){
        JsonResult json=new JsonResult();
        Long nem=deliveryService.getDeliveryCount(delivery);
        if (nem==0){
            json.setSuccess(false);
            json.setMessage("该配送员账户不存在");
        }else {
            try {
                deliveryService.deleteDeliver(delivery);
                json.setSuccess(true);
                json.setMessage("删除成功");
            }catch (Exception e){
                e.printStackTrace();
                json.setSuccess(false);
                json.setMessage("删除失败");
            }
        }
        return json;
    }

}
